package org.luxor.commons.workflow.listener;

import org.activiti.engine.delegate.event.ActivitiEntityEvent;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.luxor.commons.workflow.component.cache.CustomDeploymentCache;
import org.luxor.commons.workflow.constant.VariableConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 流程正常结束
 *
 * @author Mr.Yan  @date: 2021/10/27
 */
public class ProcessCompletedListener implements ActivitiEventHandler {

    private static final Logger log = LoggerFactory.getLogger(ProcessCompletedListener.class);

    private CustomDeploymentCache deploymentCache;

    public ProcessCompletedListener(CustomDeploymentCache deploymentCache) {
        this.deploymentCache = deploymentCache;
    }

    @Override
    public void handle(ActivitiEvent event) {
        // 清除[运行实例流程定义]
        String processDefinitionId = event.getProcessDefinitionId();
        String processInstanceId = event.getProcessInstanceId();
        deploymentCache.removeRuProcessDefinition(processDefinitionId, processInstanceId);

        ActivitiEntityEvent activitiEntity = (ActivitiEntityEvent) event;
        ExecutionEntity executionEntity = (ExecutionEntity) activitiEntity.getEntity();

        log.debug("流程正常结束(ProcessCompletedListener). ProcInstId: " + activitiEntity.getProcessInstanceId()
                + "(" + executionEntity.getVariable(VariableConst.PROCESS_NAME) + "),ExecutionId: " + activitiEntity.getExecutionId()
                + ",StartUserId: " + executionEntity.getStartUserId());
    }

}
